package cz.muni.fi.pa165.sportclubmanager.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Represents a player.
 *
 * @author Jan Bliznak
 */
@Entity
public class Player implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    @Temporal(TemporalType.DATE)
    private Date birthDate;
    private int height;
    private int weight;

    public Player() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthDate() {
        return this.birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * Todays current age in years of player.
     *
     * @return current age in years
     */
    public int getAge() {
        if (this.birthDate == null) {
            return 0;
        }
        Calendar birthCal = Calendar.getInstance();
        birthCal.setTime(this.birthDate);
        Calendar nowCal = Calendar.getInstance();
        int age = nowCal.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
        if (birthCal.get(Calendar.MONTH) > nowCal.get(Calendar.MONTH)
                || (birthCal.get(Calendar.MONTH) == nowCal.get(Calendar.MONTH) && birthCal.get(Calendar.DATE) > nowCal.get(Calendar.DATE))) {
            --age;
        }
        return age;
    }

    /**
     *
     * @return height in centimeters
     */
    public int getHeight() {
        return this.height;
    }

    /**
     * Sets height of player as units of centimeters.
     *
     * @param height non-negative value for height of player
     */
    public void setHeight(int height) {
        if (height < 0) {
            throw new IllegalArgumentException("Height cannot be negative: " + height);
        }
        this.height = height;
    }

    /**
     *
     * @return weight in kilograms
     */
    public int getWeight() {
        return this.weight;
    }

    /**
     * Sets weight of player in unit of kilograms.
     *
     * @param weight non-negative value for weight of player
     */
    public void setWeight(int weight) {
        if (weight < 0) {
            throw new IllegalArgumentException("Weight cannot be negative: " + weight);
        }
        this.weight = weight;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Player other = (Player) obj;
        if (this.id == null || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Player{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", birthDate=" + birthDate + ", height=" + height + ", weight=" + weight + '}';
    }
}